<%@page import="javax.activation.DataHandler"%>
<%@page import="javax.activation.FileDataSource"%>
<%@page import="javax.activation.DataSource"%>
<%@page import="javax.mail.internet.MimeBodyPart"%>
<%@page import="javax.mail.BodyPart"%>
<%@page import="javax.mail.internet.MimeMultipart"%>
<%@page import="bean.MemberShipVo"%>
<%@page import="bean.MemberShipDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="bean.SMTPAuthenticator"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="javax.mail.Transport" %>
<%@ page import="javax.mail.Message" %>
<%@ page import="javax.mail.internet.InternetAddress" %>
<%@ page import="javax.mail.Address" %>
<%@ page import="javax.mail.internet.MimeMessage" %>
<%@ page import="javax.mail.Session" %>
<%@ page import="javax.mail.Authenticator" %>
<%@ page import="java.util.Properties" %>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src='/kiSunnyShop/js/main.js'></script>
<title>Insert title here</title>
<style>
body{
/*m_f_membership*/

}
#wrapper1{width:300%; height:600px;background:#223B6E;background-image:url(/kiSunnyShop/images/mem_bg.jpg); }
/* form h1 { margin-bottom:5%; border-bottom:2px solid #242424;  padding-bottom: 5px; color:#223B6E;} */
/* .centerBlock { text-align: center; } */
ul {list-style:none;}

 ul li label {font-weight: bold; color: #666; float: left; width:150px;display:block; } 
.btnStyle {color: #FFF; background-color: #666; padding-top: 5px; padding-right: 10px; 
padding-bottom: 5px; padding-left: 10px; border: 1px solid #000; }
#pos{margin:20px 350px; padding: 370px 10px;width:800px; height:250px;} 
#btn_m{margin-left:100%;margin-top:2%;} 
</style>
</head>
<body>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="m" class="bean.MemberShipVo" scope="page"/>
<jsp:setProperty property="*" name="m"/>

<%
MemberShipDTO dto = new MemberShipDTO();
	dto.setVo(m);
	boolean b =dto.insert();
	//if(dto.insert()){%>
<%request.setCharacterEncoding("UTF-8");

String sender = request.getParameter("sender");
String email = request.getParameter("email");
String subject = request.getParameter("subject");
String content = request.getParameter("content");

//정보를 담기 위한 객체
Properties p = new Properties();

//SMTP 서버의 계정 설정
//Naver와 연결할 경우 네이버 아이디 지정
//Google과 연결할 경우 본인의 Gmail 주소
p.put("mail.smtp.user", "wiill82");

p.setProperty("mail.transport.protocol", "smtp");
p.setProperty("mail.host", "smtp.naver.com");
p.setProperty("mail.user", "goo82gle82");
p.setProperty("mail.password", "t4i5m8s3gfy");

//SMTP 서버 정보 설정
//네이버일 경우 smtp.naver.com
//Google일 경우 smtp.gmail.com
p.put("mail.smtp.host", "smtp.naver.com");
	
//아래 정보는 네이버와 구글이 동일하므로 수정하지 마세요.
p.put("mail.smtp.port", "465");
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.debug", "true");
p.put("mail.smtp.socketFactory.port", "465");
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.smtp.socketFactory.fallback", "false");

try {
	Authenticator auth = new SMTPAuthenticator();
	Session ses = Session.getInstance(p, auth);

	// 메일을 전송할 때 상세한 상황을 콘솔에 출력한다.
	ses.setDebug(true);
	Transport transport = ses.getTransport();	
	// 메일의 내용을 담기 위한 객체
	MimeMessage msg = new MimeMessage(ses);

	// 제목 설정
	msg.setSubject(subject);
		
	// 보내는 사람의 메일주소
	Address fromAddr = new InternetAddress(sender);
	msg.setFrom(fromAddr);
		
	// 받는 사람의 메일주소
	Address toAddr = new InternetAddress(email);
	msg.addRecipient(Message.RecipientType.TO, toAddr);
		
	//---------------------<사진 관련>-------------------------
    MimeMultipart multipart = new MimeMultipart("related");

    // first part  (the html)
    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlText = "<img src=\"cid:image\">";
   
    messageBodyPart.setContent(htmlText, "text/html");

    // add it
    multipart.addBodyPart(messageBodyPart);
    
    // second part (the image)
    messageBodyPart = new MimeBodyPart();
//     DataSource fds = new FileDataSource("D:/jsy/kiSunnyShop/WebContent/images/membership_email.jpg");
 	DataSource fds = new FileDataSource("F:/자바/학원/and_1406/2/kiSunnyShop/WebContent/images/membership_email.jpg");
    messageBodyPart .setDataHandler(new DataHandler(fds));
    messageBodyPart .setHeader("Content-ID","<image>");

    // add it
    multipart.addBodyPart(messageBodyPart );

   
    
    // put everything together
    msg.setContent(multipart);
	
    transport.connect();
    transport.sendMessage(msg,
        msg.getRecipients(Message.RecipientType.TO));
   	transport.close();
		
} catch (Exception mex) {
	mex.printStackTrace();
	String script = "<script type='text/javascript'>\n";
	script += "alert('메일발송에 실패했습니다.');\n";
	script += "history.back();\n";
	script += "</script>";
	out.print(script);
	return;
}
	
String script = "<script type='text/javascript'>\n";
script += "alert('메일발송에 성공했습니다.');\n";
script += "</script>";
out.print(script);

%>	
	
	
	
	
	
	
<div id="wrapper1" >
<form>
	<ul>
	<div id="pos">
		<li><label><font color="#1cd0eb">아이디:</font></label> 
			<label><font color="#4f5458"><%=dto.getVo().getMid() %></font></label></li><br/><br/>
		
		<li><label><font color="#1cd0eb">E-mail:</font></label>
		 	<label><font color="#4f5458"><%=dto.getVo().getEmail() %></font></label></li><br/><br/><br/>
		<li><label>	<input type ="button" id='btn_m' class="btnStyle" value="메인 화면"/></label></li>
		
		<!-- qustion 받기 -->
<!-- 		<li><label><font color="#1cd0eb">question:</font></label>  -->
<%-- 			<label><font color="#4f5458"><%=dto.getVo().getQuestion() %></font></label></li><br/><br/> --%>
		
<!-- 		<li><label><font color="#1cd0eb">answer:</font></label>  -->
<%-- 			<label><font color="#4f5458"><%=dto.getVo().getAnswer() %></font></label></li><br/><br/> --%>
		
	</div>
	</ul>
</form>		
</div>

<script>inputInit1();</script>
</body>
</html>